package com.example.librarymanagement.mapper;

import com.example.librarymanagement.model.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Optional;

@Mapper
public interface UserMapper {
    @Insert("INSERT INTO Users (username, password) VALUES (#{username}, #{password})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertUser(User user);
    @Select("SELECT * FROM Users WHERE username = #{username} AND password = #{password}")
    User findUserByUsernameAndPassword(String username, String password);
    @Select("SELECT * FROM users WHERE id = #{id}")
    Optional<User> findById(@Param("id") Integer id);

    @Update("UPDATE users SET username = #{username}, password = #{password} WHERE id = #{id}")
    int update(@Param("id") Integer id, @Param("username") String username, @Param("password") String password);
    @Select("SELECT * FROM users WHERE username = #{username}")
    Optional<User> getUserByUsername(String username);
    @Select("SELECT EXISTS(SELECT 1 FROM users WHERE username = #{username})")
    boolean isUsernameExists(@Param("username") String username);
    @Select("SELECT * FROM users")
    List<User> findAllUsers();
}